<?php

namespace  app\utils;

use app\model\BaseModel;

class CamlHump
{

    //下划线命名到驼峰命名
    static function toCamelCase($str)
    {
        $array = explode('_', $str);
        $result = $array[0];
        $len = count($array);
        if ($len > 1) {
            for ($i = 1; $i < $len; $i++) {
                $result .= ucfirst($array[$i]);
            }
        }
        return $result;
    }

    static  function toCamlObj($obj)
    {

        if($obj instanceof BaseModel){
            return $obj->toArray();
        }

        $newObj = [];
        if(empty($obj)){
            return [];
        }
        foreach ($obj as $key => $value) {
            $newObj[static::toCamelCase($key)] = $value;
        }
        return $newObj;
    }
    static  function toCamlArray($array)
    {
        $newArray = [];
        for ($i = 0; $i < count($array); $i++) {
            array_push($newArray, static::toCamlObj($array[$i]));
        }
        return $newArray;
    }

    static function now(){
        return date("Y-m-d H:i:s",time());
    }
}
